import request, { type ListResponse, type ListQuery } from "@/utils/request";

const SHIPPING_TEMPLATE_BASE_URL = "/api";

const ShippingTemplateAPI = {
  getShippingTemplateList: (params: ShippingTemplateListParams) => {
    return request.get<any, ListResponse<ShippingTemplateType>>(
      `${SHIPPING_TEMPLATE_BASE_URL}/express/templates/`,
      {
        params,
      }
    );
  },
  addShippingTemplate: (data: any) => {
    return request.post(`${SHIPPING_TEMPLATE_BASE_URL}/express/templates/`, data);
  },
  updateShippingTemplate: (data: Partial<ShippingTemplateType>) => {
    return request.put(`${SHIPPING_TEMPLATE_BASE_URL}/express/templates/${data.id}/`, data);
  },
  deleteShippingTemplate: (id: number) => {
    return request.delete(`${SHIPPING_TEMPLATE_BASE_URL}/express/templates/${id}/`);
  },
};

export type ShippingTemplateListParams = {
  search: string;
} & ListQuery;

export type ShippingTemplateIetmType = "quantity" | "volume" | "weight";

export type ShippingTemplateType = {
  id: number;
  created_at: string;
  updated_at: string;
  is_active: boolean;
  name: string;
  type: ShippingTemplateIetmType;
  sequence: number;
  enable_free: boolean;
  enable_no_service: boolean;
  user: number;
};

export type ShippingTemplateDomainType = {
  address_ids: number[];
  first_unit: number;
  first_amount: number;
  next_unit: number;
  next_amount: number;
  is_global: boolean;
};

export type ShippingTemplateFreeDomainType = {
  free_unit: number;
  free_amount: number;
  address_ids: number[];
};

export type ShippingTemplateNoServiceDomainType = {
  address_ids: number[];
};

export type ShippingTemplateCreateType = {
  domains: ShippingTemplateDomainType[];
  free_domains: ShippingTemplateFreeDomainType[];
  no_service_domains: ShippingTemplateNoServiceDomainType[];
} & Partial<ShippingTemplateType>;

export default ShippingTemplateAPI;
